import _ from 'underscore'
import moment from 'moment'

import { Scope } from 'lib/scopeFactory'
import dispatcher from 'lib/dispatcher'

export const naturalDays = [
  {
    value: {
      spanTime: 0,
      endTime: moment().subtract(1, 'day').startOf('day').valueOf()
    },
    alias: '1 day',
    text: '昨天'
  },
  {
    value: {
      spanTime: 0,
      endTime: moment().startOf('day').valueOf()
    },
    alias: '0 day',
    text: '今天'
  },
  {
    value: {
      spanTime: moment.duration(6, 'days').valueOf(),
      endTime: moment().subtract(1, 'week').endOf('week').startOf('day').valueOf()
    },
    alias: 'last week',
    text: '上周'
  },
  {
    value: {
      spanTime: moment().startOf('day').valueOf() - moment().startOf('week').valueOf(),
      endTime: moment().startOf('day').valueOf()
    },
    alias: '0 week',
    text: '本周'
  },
  {
    value: {
      spanTime: moment().subtract(1, 'month').endOf('month').startOf('day').valueOf() - moment().subtract(1, 'month').startOf('month').valueOf(),
      endTime: moment().subtract(1, 'month').endOf('month').startOf('day').valueOf()
    },
    alias: 'last month',
    text: '上月'
  },
  {
    value: {
      spanTime: moment().startOf('day').valueOf() - moment().startOf('month').valueOf(),
      endTime: moment().startOf('day').valueOf()
    },
    alias: '0 month',
    text: '本月'
  },
  {
    value: {
      spanTime: moment().subtract(1, 'year').endOf('year').startOf('day').valueOf() - moment().subtract(1, 'year').startOf('year').valueOf(),
      endTime: moment().subtract(1, 'year').endOf('year').startOf('day').valueOf()
    },
    alias: 'last year',
    text: '上年'
  },
  {
    value: {
      spanTime: moment().startOf('day').valueOf() - moment().startOf('year').valueOf(),
      endTime: moment().startOf('day').valueOf()
    },
    alias: '0 year',
    text: '本年'
  }
];

export const relativeDays = [
  {
    value: {
      spanTime: moment.duration(6, 'days').valueOf(),
      endTime: moment().subtract(1, 'days').startOf('day').valueOf()
    },
    alias: '7 day',
    text: '过去7天'
  },
  {
    value: {
      spanTime: moment.duration(29, 'days').valueOf(),
      endTime: moment().subtract(1, 'days').startOf('day').valueOf()
    },
    alias: '30 day',
    text: '过去30天'
  },
  {
    value: {
      spanTime: moment.duration(89, 'days').valueOf(),
      endTime: moment().subtract(1, 'days').startOf('day').valueOf()
    },
    alias: '90 day',
    text: '过去90天'
  },
  {
    value: {
      spanTime: moment.duration(179, 'days').valueOf(),
      endTime: moment().subtract(1, 'days').startOf('day').valueOf()
    },
    alias: '180 day',
    text: '过去180天'
  },
  {
    value: {
      spanTime: moment.duration(364, 'days').valueOf(),
      endTime: moment().subtract(1, 'days').startOf('day').valueOf()
    },
    alias: '365 day',
    text: '过去365天'
  }
];

export function getDayFromFunction(func = p => false) {
  let item = _.find(naturalDays, func);
  if (item) {
    return {
      isNatural: true,
      item
    }
  }

  item = _.find(relativeDays, func);
  if (item) {
    return {
      isNatural: false,
      item
    }
  }

  return null;
};

// export default new DateRangeStoreEnhanced()
export function getDayFromAlias(alias) {
  return getDayFromFunction(p => p.alias === alias);
}

export default class DateRangeStoreEnhanced extends Scope {
  constructor(props) {
    super(props);

    this.$state = {
      naturalDays: naturalDays,
      relativeDays: relativeDays,
      MINDate: moment().startOf('day').subtract(3, 'months').valueOf(),
      MAXDate: moment().startOf('day').valueOf(),
      minDate: moment().startOf('day').subtract(3, 'months').valueOf(),
      maxDate: moment().startOf('day').valueOf(),
      isOpen: false,
      isNatural: props.isNatural ? props.isNatural : false,
      isCustom: props.isCustom ? props.isCustom : false,
      selectedItem: this.getSelectedItem(props)
    };

    if (props.spanTime !== undefined && props.endTime !== undefined) {
      this.$state.value = {
        spanTime: props.spanTime,
        endTime: props.endTime
      };
    }
    else {
      this.$state.value = {
        spanTime: moment().startOf('day').valueOf() - moment().startOf('week').valueOf(),
        endTime: moment().startOf('day').valueOf()
      };
    }

    this.setDisplayText(this.$state.selectedItem.value, props);

    this.$state.leftMaxDate = this.$state.maxDate;
    this.$state.leftSelectDate = (props.spanTime && props.endTime) ? (props.endTime - props.spanTime) : this.$state.minDate;

    this.$state.rightMinDate = this.$state.minDate;
    this.$state.rightSelectDate = props.endTime ? props.endTime : this.$state.maxDate;
  }

  $receive(props) {
    _.extend(this.$state, {
      isNatural: props.isNatural ? props.isNatural : false,
      isCustom: props.isCustom ? props.isCustom : false,
      selectedItem: this.getSelectedItem(props)
    });

    if (props.spanTime !== undefined && props.endTime !== undefined) {
      this.$state.selectedItem.value = {
        spanTime: props.spanTime,
        endTime: props.endTime
      };
    }
    else {
      this.$state.selectedItem.value = {
        spanTime: moment().startOf('day').valueOf() - moment().startOf('week').valueOf(),
        endTime: moment().startOf('day').valueOf()
      };
    }

    this.setDisplayText(this.$state.selectedItem.value, props);
    this.emit();
  } // $receive

  getSelectedItem(props) {
    let item = relativeDays[0];
    if (props.alias) {
      let found = getDayFromAlias(props.alias);
      if (found) {
        item = found.item;
      }
    }
    return item;
  } // getSelectedItem

  setDisplayText(value = this.$state.selectedItem.value, props = this.viewInstance.props) {
    if (props.showAlias && !this.$state.isCustom) {
      if (this.$state.isNatural) {
        let item = _.find(this.$state.naturalDays,
          p => p.value.endTime === value.endTime && p.value.spanTime === value.spanTime);
        this.$state.displayText = item ? item.text : 'unknown';
      } else {
        let item = _.find(this.$state.relativeDays,
          p => p.value.endTime === value.endTime && p.value.spanTime === value.spanTime);
        this.$state.displayText = item ? item.text : 'unknown';
      }
    } else {
      let beginTime = moment(value.endTime - value.spanTime).format('YYYY-MM-DD');
      let endTime = moment(value.endTime).format('YYYY-MM-DD');
      this.$state.displayText = beginTime + ' ~ ' + endTime;
    }
  }

  $state_changed(payload) {
    _.extend(this.$state, payload.data);
    this.emit();
  }

  $select_item({ data }) {
    _.extend(this.$state, data);
    this.setDisplayText();

    this.emit(() => {
      let parameter = {
        isNatural: this.$state.isNatural,
        isCustom: this.$state.isCustom,
        item: this.$state.selectedItem
      };

      let action = this.viewInstance.props.onCompleted;
      if (action) {
        action(parameter);
      }
    });
  }
}
